package com.imcrl.piano.mvc.controller
{
	import com.imcrl.piano.core.Assets;
	import com.imcrl.piano.mvc.model.MusicModel;
	import com.imcrl.piano.mvc.model.SoundModel;
	import com.imcrl.piano.mvc.model.vo.NoetsDefVO;
	import com.imcrl.piano.mvc.model.vo.NoetsStack;
	import com.imcrl.piano.mvc.view.PianoView;
	import com.imcrl.piano.mvc.view.listItem.NoetsItemRender;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	public class PianoPlayerController
	{
		protected var model:MusicModel;
		protected var view:PianoView;
		
		private var noetsStack:NoetsStack;
		private var soundModel:SoundModel;
		
		private var soundController:BgSoundController;
		
		private var keydownList:Dictionary;
		public function PianoPlayerController()
		{
			soundModel=new SoundModel();
			
			keydownList=new Dictionary();
			
			
			soundController=new BgSoundController();
		}
		
		
		public function setModel(value:MusicModel):void{
			this.model=value;
			
			
			var key:int;
			var soundClass:Class;
			
			soundModel.clear();
			for each(var item:NoetsDefVO in this.model.data){
				key=item.soundKey;
				
				soundClass=Assets.getSound(key);
				soundModel.regist(key,new soundClass());
			}
			noetsStack=new NoetsStack(value.data);
		}
		public function setView(value:PianoView):void{
			this.view=value;
	
			this.view.startBtn.addEventListener(MouseEvent.CLICK,start);
		}
		
		public function start(event:Event):void{
			this.view.startBtn.visible=false;
			
			this.view.data=this.model.data;
			this.view.start();
			
			soundController.start(model.bgSoundPath);
			soundController.addEventListener(Event.COMPLETE,soundCompleteHandler);
			
			this.view.stage.addEventListener(KeyboardEvent.KEY_UP,keyupHandler);
			this.view.stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownHandler);
			
		}
		
		private function soundCompleteHandler(event:Event):void{
			this.view.stop();
			
			
			soundController.removeEventListener(Event.COMPLETE,soundCompleteHandler);
			
			this.view.stage.removeEventListener(KeyboardEvent.KEY_UP,keyupHandler);
			this.view.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keydownHandler);
		}
		
		
		private function keyupHandler(event:KeyboardEvent):void{
			var item:NoetsDefVO=keydownList[event.keyCode];
			if(item==null){
				return;
			}
			keydownList[event.keyCode]=null;
			delete keydownList[event.keyCode];
			
			this.soundModel.stopByKey(item.soundKey);
			this.view.up(item.node);
		}
		
		
		/**
		 * 当按下键盘时; 
		 * @param event
		 * 
		 */		
		private function keydownHandler(event:KeyboardEvent):void{
			var channel:int=model.channels[event.keyCode];
			if(channel==0)return;
			
			var startTime:int=getTimer();
			
			//已经按下时,不重复触发;
			if(keydownList[event.keyCode] != null)return;
			
			var item:NoetsDefVO;
			var bool:Boolean=false;
			
			var sub:int;
			while(noetsStack.hasNext){
				item=noetsStack.next().get();
				
				sub=item.start-view.position;
				if(sub+item.duration<-100){//给他个100毫秒的误差;
					continue;
				}else if(sub>1000){
					noetsStack.back();
					break;
				}else{
					bool=check(sub,channel,item);
					break;
				}
			}
			
			
			
			if(bool==false){
				noetsStack.back();
			}
			
			if(item==null || bool==false )return;
			
			item.pass=bool;
			this.soundModel.playByKey(item.soundKey);
			
			this.view.down(item.node);
			
			keydownList[event.keyCode]=item;
			
		}
		
		
		/**
		 * 
		 * @param sub 偏差值 
		 * @param channel 
		 * @param item 音符对像;
		 * @return 
		 * 
		 */		
		private function check(sub:int,channel:int,item:NoetsDefVO):Boolean{
			if(channel !=item.node)return false;
			
			//评分计算;
			view.scoringView.check(sub);
			
			return true;
		}
		
		
		public function dispose():void{
			
		}
		
	}
}